package com.company.cloud.acct.modules.mgr.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.company.cloud.acct.modules.base.entity.BankCardInfo;
import com.company.cloud.acct.modules.base.service.BankCardInfoService;
import com.company.cloud.acct.modules.cache.models.BankCardModel;
import com.company.cloud.acct.modules.cache.models.CashConfigModel;
import com.company.cloud.acct.modules.cache.utils.AcctConvertUtils;
import com.company.cloud.acct.modules.mgr.controller.req.BankCardQueryParam;
import com.company.cloud.common.auth.annotation.Authc;
import com.company.cloud.common.bean.BaseResult;
import com.company.cloud.common.bean.LoginSimpleUserInfo;
import com.company.cloud.common.constant.BaseReturnEnum;
import com.company.cloud.common.exception.BusinessException;
import com.company.cloud.common.utils.AuthContextUtils;
import com.company.cloud.common.utils.CheckUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.function.Function;

@Authc
@RestController
@RequestMapping("/mgr/acct/bankcard")
@Api(tags="Acct_MGR_银行卡管理")
public class BankCardMgrController {
    @Autowired
    private BankCardInfoService bankCardInfoService;

    @PostMapping("/query")
    @ApiOperation(value="查询银行卡列表",response = BankCardModel.class)
    public BaseResult query(@RequestBody BankCardQueryParam params) {
        LoginSimpleUserInfo userInfo= AuthContextUtils.getUser();
        IPage<BankCardInfo> page = bankCardInfoService.page(new Page<>(params.getPage(), params.getRows()), new QueryWrapper<BankCardInfo>()
                .eq("company_no", userInfo.getCompanyNo())
                .like(CheckUtils.hasText(params.getBankName()), "bank_name", params.getBankName())
                .eq(CheckUtils.hasText(params.getBankCode()), "bank_code", params.getBankCode())
                .eq(CheckUtils.hasText(params.getCardNo()), "card_no", params.getCardNo())
                .eq(CheckUtils.hasText(params.getCardName()), "card_name", params.getCardName())
                .eq(CheckUtils.hasText(params.getCardType()), "card_type", params.getCardType())
                .like(CheckUtils.hasText(params.getCardUserName()), "card_user_name", params.getCardUserName())
                .eq(CheckUtils.hasText(params.getCardMobile()), "card_mobile", params.getCardMobile())
                .eq(CheckUtils.hasText(params.getCardIdCard()), "card_id_card", params.getCardIdCard())
        );
        IPage<BankCardModel> data = page.convert(new Function<BankCardInfo, BankCardModel>() {
            @Override
            public BankCardModel apply(BankCardInfo entity) {
                return AcctConvertUtils.getBankCardModel(entity);
            }
        });
        return BaseResult.ok(data);
    }

    @GetMapping("/get")
    @ApiOperation(value="查询银行卡详情",response = BankCardModel.class)
    public BaseResult get(@RequestParam("bankCardId") Integer bankCardId) {
        BankCardInfo entity = bankCardInfoService.getById(bankCardId);
        if (null == entity) {
            throw new BusinessException(BaseReturnEnum.DATA_NOT_EXISTS);
        }
        return BaseResult.ok(AcctConvertUtils.getBankCardModel(entity));
    }

}
